<template>
	<view>
		<view class="inpu-form-main">
			<view class="input-item">
				<view class="item-zhang item-form-top">
						<input type="text" placeholder="输入手机号" v-model="phone"  />
				</view>
				<view class="item-yzm item-form-top" >
						<view class="yzm-left">
									<input type="text" placeholder="输入验证码" maxlength="6" v-model="code" />
						</view>
						<view class="yzm-right " @click="getCode">
								{{time==0?'获取验证码':(time+'s后重新获取')}}
						</view>
				</view>
			</view>
			<view class="form-inputmadin" @click="finish">提交</view>
		</view>
	</view>
</template>


<script>
	import * as Api from '@/api/code.js'
	import * as userApi from '@/api/user.js'
	export default{
		data(){
			return{
				phone:"",
				code:"",
				time:0,
				interval:null,
			}
		},
		onShow() {
			
		},
		onLoad() {
			
		},
		methods:{
			getCode(){
				let _this = this
				if(!this.phone){
					return
				}
				if(this.$fxLink.checkPhone(this.phone)){
					this.$toast("请输入正确的手机号")
					return
				}
				Api.sendBindMobileMessage({
					wxapp_id: _this.$wxapp_id,
					mobile:this.phone,
				}).then(result => {
						if(result.code === 1){
							uni.setStorageSync("phoneCode",this.phone)
							uni.navigateTo({
								url:"/pages/user/setup/get_code"
							})
						}else{
							this.$toast(result.msg)
						}
				})
			},
			finish(){
				let _this = this
				let token = uni.getStorageSync("AccessToken")
				if(this.$fxLink.checkPhone(this.phone)){
					this.$toast("请输入正确的手机号")
					return
				}
				if(!this.code){
					this.$toast("请输入验证码")
					return
				}
				userApi.phone_obj({
					token,
					wxapp_id: _this.$wxapp_id,
					phone:_this.phone,
					code:_this.code
				}).then(result => {
					_this.$toast(result.msg)
					if(result.code == 1){
						setTimeout(() => {
							// #ifdef H5
							uni.switchTab({
								url:"/pages/my/my"
							})
							// #endif
							// #ifndef H5
							uni.navigateBack({
								delta:1
							})
							// #endif
						},1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #F2F7FA;
	}
</style>